package com.bjc.sdx.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjc.sdx.R;
import com.bjc.sdx.model.Fruit;

import java.util.ArrayList;

public class MyGridAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<Fruit> list;

    public MyGridAdapter(LayoutInflater inflater, ArrayList<Fruit> list) {
        this.inflater = inflater;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit =list.get(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view=inflater.inflate(R.layout.fruit_gridview, null);
            viewHolder=new ViewHolder();
            viewHolder.image=(ImageView) view.findViewById(R.id.imageview);
            viewHolder.name=(TextView) view.findViewById(R.id.textview);
            view.setTag(viewHolder);
        }else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }
        viewHolder.image.setImageResource(fruit.getImage());
        viewHolder.name.setText(fruit.getName());
        return view;
    }
    class ViewHolder{
        ImageView image;
        TextView name;
    }
}
